package cn.edu.klmy.controller;

import cn.edu.klmy.dao.*;
import cn.edu.klmy.models.course.Course;
import cn.edu.klmy.models.elective.ElectiveCourseItem;
import cn.edu.klmy.models.elective.ElectiveCourseSituation;
import cn.edu.klmy.models.elective.StudentCourseElective;
import cn.edu.klmy.models.elective.StudentCourseElectiveOneWeek;
import cn.edu.klmy.models.param.SystemParam;
import cn.edu.klmy.models.schedule.RestrictionTime;
import cn.edu.klmy.models.student.Student;
import cn.edu.klmy.spring.security.core.userdetails.CourseSystemUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 顾伟
 *         创建日期：13-5-15
 */
@Controller
@RequestMapping("/elective")
public class ElectiveController {
    @Autowired
    private IElectiveDao electiveDao;

    @Autowired
    private IStudentDao studentDao;

    @Autowired
    private ICourseDao courseDao;

    @Autowired
    private ITeacherDao teacherDao;

    @Autowired
    private IScheduleDao scheduleDao;

    @RequestMapping("/list/situation/{schedulingId}")
    @ResponseBody
    public List<ElectiveCourseSituation> loadElectiveCourseSituation(HttpServletRequest request, @PathVariable int schedulingId) {
        HttpSession session = request.getSession();
        SystemParam systemParam = (SystemParam) session.getAttribute("param");
        int term = systemParam.getTerm();
        int section = systemParam.getSection();

        return electiveDao.getCourseElection(schedulingId, term, section);
    }

    @RequestMapping("/list/table")
    @ResponseBody
    public List<StudentCourseElectiveOneWeek> loadCourseTable(HttpServletRequest request) {
        HttpSession session = request.getSession();
        SystemParam systemParam = (SystemParam) session.getAttribute("param");
        String studentIDNo = ((CourseSystemUser) SecurityContextHolder.getContext()
                .getAuthentication().getPrincipal()).getUser().getUserIdNo();
        Student student = studentDao.getStudent("STUDENT_ID_NO", "'" + studentIDNo + "'");
        int studentId = 0;
        if (null != student)
            studentId = student.getId();
        int term = systemParam.getTerm();
        int section = systemParam.getSection();
        List<StudentCourseElective> studentCourseElectiveList = electiveDao.getStudentCurrentElection(studentId, term, section);

        List<StudentCourseElectiveOneWeek> studentCourseElectiveOneWeekList = new ArrayList<StudentCourseElectiveOneWeek>();


        StudentCourseElectiveOneWeek firstLessonOneWeek = new StudentCourseElectiveOneWeek("第一节课");
        StudentCourseElectiveOneWeek secondLessonOneWeek = new StudentCourseElectiveOneWeek("第二节课");
        StudentCourseElectiveOneWeek thirdLessonOneWeek = new StudentCourseElectiveOneWeek("第三节课");
        StudentCourseElectiveOneWeek forthLessonOneWeek = new StudentCourseElectiveOneWeek("第四节课");
        StudentCourseElectiveOneWeek fifthLessonOneWeek = new StudentCourseElectiveOneWeek("第五节课");
        StudentCourseElectiveOneWeek sixthLessonOneWeek = new StudentCourseElectiveOneWeek("第六节课");
        StudentCourseElectiveOneWeek seventhLessonOneWeek = new StudentCourseElectiveOneWeek("第七节课");
        StudentCourseElectiveOneWeek eighthLessonOneWeek = new StudentCourseElectiveOneWeek("第八节课");

        for (StudentCourseElective studentCourseElective : studentCourseElectiveList) {
            int time = studentCourseElective.getElectiveTime();
            switch (time) {
                case 1:
                    setWeekCourseElective(studentCourseElective, firstLessonOneWeek);
                    break;
                case 2:
                    setWeekCourseElective(studentCourseElective, secondLessonOneWeek);
                    break;
                case 4:
                    setWeekCourseElective(studentCourseElective, thirdLessonOneWeek);
                    break;
                case 8:
                    setWeekCourseElective(studentCourseElective, forthLessonOneWeek);
                    break;
                case 16:
                    setWeekCourseElective(studentCourseElective, fifthLessonOneWeek);
                    break;
                case 32:
                    setWeekCourseElective(studentCourseElective, sixthLessonOneWeek);
                    break;
                case 64:
                    setWeekCourseElective(studentCourseElective, seventhLessonOneWeek);
                    break;
                case 128:
                    setWeekCourseElective(studentCourseElective, eighthLessonOneWeek);
                    break;
            }
        }

        RestrictionTime restrictionTime = scheduleDao.getRestrictionTimeRule();
        if (restrictionTime.isHasFirstLesson()) {
            studentCourseElectiveOneWeekList.add(firstLessonOneWeek);
        }

        if (restrictionTime.isHasSecondLesson()) {
            studentCourseElectiveOneWeekList.add(secondLessonOneWeek);
        }

        if (restrictionTime.isHasThirdLesson()) {
            studentCourseElectiveOneWeekList.add(thirdLessonOneWeek);
        }

        if (restrictionTime.isHasForthLesson()) {
            studentCourseElectiveOneWeekList.add(forthLessonOneWeek);
        }

        if (restrictionTime.isHasFifthLesson()) {
            studentCourseElectiveOneWeekList.add(fifthLessonOneWeek);
        }

        if (restrictionTime.isHasSixthLesson()) {
            studentCourseElectiveOneWeekList.add(sixthLessonOneWeek);
        }

        if (restrictionTime.isHasSeventhLesson()) {
            studentCourseElectiveOneWeekList.add(seventhLessonOneWeek);
        }

        if (restrictionTime.isHasEighthLesson()) {
            studentCourseElectiveOneWeekList.add(eighthLessonOneWeek);
        }

        return studentCourseElectiveOneWeekList;

    }

    @RequestMapping("/list/course/{week}/{time}")
    @ResponseBody
    public List<ElectiveCourseItem> loadElectiveCourseData(HttpServletRequest request, @PathVariable int week, @PathVariable int time) {
        HttpSession session = request.getSession();
        SystemParam systemParam = (SystemParam) session.getAttribute("param");
        String studentIDNo = ((CourseSystemUser) SecurityContextHolder.getContext()
                .getAuthentication().getPrincipal()).getUser().getUserIdNo();
        Student student = studentDao.getStudent("STUDENT_ID_NO", "'" + studentIDNo + "'");
        int studentId = 0;
        if (null != student)
            studentId = student.getId();
        int term = systemParam.getTerm();
        int section = systemParam.getSection();
        return electiveDao.getElectiveCourse(studentId, term, section, week, time);
    }

    private void setWeekCourseElective(StudentCourseElective studentCourseElective, StudentCourseElectiveOneWeek studentCourseElectiveOneWeek) {
        int week = studentCourseElective.getElectiveWeek();
        switch (week) {
            case 1:
                studentCourseElectiveOneWeek.setMondayCourse(studentCourseElective);
                break;
            case 2:
                studentCourseElectiveOneWeek.setTuesdayCourse(studentCourseElective);
                break;
            case 4:
                studentCourseElectiveOneWeek.setWednesdayCourse(studentCourseElective);
                break;
            case 8:
                studentCourseElectiveOneWeek.setThursdayCourse(studentCourseElective);
                break;
            case 16:
                studentCourseElectiveOneWeek.setFridayCourse(studentCourseElective);
                break;
            case 32:
                studentCourseElectiveOneWeek.setSaturdayCourse(studentCourseElective);
                break;
            case 64:
                studentCourseElectiveOneWeek.setSundayCourse(studentCourseElective);
                break;
        }
    }

    @RequestMapping("/course/info/{courseId}/teacher/{teacherId}")
    public String getCourseInfo(@PathVariable int courseId, @PathVariable int teacherId, Model model) {
        List<Course> courseList = courseDao.getCourses("ID", courseId);
        if (courseList != null && courseList.size() > 0) {
            model.addAttribute("courseInfo", courseDao.getCourses("ID", courseId).get(0));
            model.addAttribute("teacherId", teacherId);
        }
        return "courseInfo";
    }

    @RequestMapping("/isElectable/{scheduleId}/{week}/{time}")
    @ResponseBody
    public boolean isCourseElectable(HttpServletRequest request, @PathVariable int scheduleId, @PathVariable int week, @PathVariable int time) {
        HttpSession session = request.getSession();
        SystemParam systemParam = (SystemParam) session.getAttribute("param");
        int term = systemParam.getTerm();
        int section = systemParam.getSection();
        int isMore = electiveDao.isMoreElectiveNum(scheduleId, term, section, week, time);
        if (isMore == 1) {
            return true;
        }
        return false;
    }

    @RequestMapping("/add/{scheduleId}/{week}/{time}")
    @ResponseBody
    public boolean addCourseElective(HttpServletRequest request, @PathVariable int scheduleId, @PathVariable int week, @PathVariable int time) {
        HttpSession session = request.getSession();
        SystemParam systemParam = (SystemParam) session.getAttribute("param");
        String studentIDNo = ((CourseSystemUser) SecurityContextHolder.getContext()
                .getAuthentication().getPrincipal()).getUser().getUserIdNo();
        Student student = studentDao.getStudent("STUDENT_ID_NO", "'" + studentIDNo + "'");
        int studentId = 0;
        if (null != student)
            studentId = student.getId();
        int term = systemParam.getTerm();
        int section = systemParam.getSection();

        int count = electiveDao.addElectiveCourse(studentId, scheduleId, term, section, week, time);
        if (count > 0) {
            return true;
        }
        return false;
    }


    @RequestMapping("/update/{id}/{scheduleId}")
    @ResponseBody
    public boolean updateCourseElective(@PathVariable int id, @PathVariable int scheduleId) {
        int count = electiveDao.updateElectiveCourse(id, scheduleId);
        if (count > 0) {
            return true;
        }
        return false;
    }

    @RequestMapping("/clear/course/elective/data/{scheduleId}/{week}/{time}")
    @ResponseBody
    public boolean clearCourseElectiveData(HttpServletRequest request, @PathVariable int scheduleId, @PathVariable int week, @PathVariable int time) {
        HttpSession session = request.getSession();
        SystemParam systemParam = (SystemParam) session.getAttribute("param");
        int term = systemParam.getTerm();
        int section = systemParam.getSection();

        int count = electiveDao.clearCourseElectiveData(scheduleId, term, section, week, time);
        if (count > 0) {
            return true;
        }
        return false;
    }

    @RequestMapping("/delete/{id}")
    @ResponseBody
    public boolean removeCourseElective(@PathVariable int id) {
        int count = electiveDao.deleteElectiveCourse(id);
        if (count > 0) {
            return true;
        }
        return false;
    }
}
